package com.tm.service;

import com.tm.seurity.domain.User;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lichuan on 2022-09-24.
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    //TODO 调用对应的mapper service 查询当前用户信息

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {



        User  user = null;
        //TODO 查询用户信息
        if(username.equals("zhangsan")){
            user = new User();
            user.setId(1L);
            user.setUsername("zhangsan");
            user.setPassword(new BCryptPasswordEncoder().encode("123456"));
            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("ROLE_admin");
            authorities.add(simpleGrantedAuthority);
            user.setAuthorities(authorities);
        }


        return user;
    }
}
